#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "EndScene.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    rootNode = CSLoader::createNode("MainScene.csb");
    next_layer = CSLoader::createNode("next-layer.csb");
    countDown_layer = CSLoader::createNode("CountDown.csb");
    
    addChild(rootNode);
    addChild(next_layer);
    addChild(countDown_layer);
    rootNode->setColor(Color3B(36, 36, 36));
    next_layer->setVisible(false);
    
    index = 1;
    flag = true;
    isEnd = false;
    curTime = 4;
    isSuc= false;
    count =2;
    
    schedule(schedule_selector(HelloWorld::countDown),1.0f,2,0);


    return true;
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::push(float dt)
{
    if(index2 == 0)
    {
        isSuc = true;
        rootNode->setColor(Color3B(36, 36, 36));
        next_layer->setVisible(true);
        
        return;
    }
    if(!flag)
    {
        auto ss = static_cast<Sprite*>(rootNode->getChildByName(StringUtils::format("Sprite_%d",26-this->index2)));
        ss->setVisible(false);
        auto s = static_cast<Sprite*>(rootNode->getChildByName(StringUtils::format("Sprite_2_%d",26-this->index2)));
        s->setVisible(true);
        this->index2 --;
    }
    
}

void HelloWorld::countDown(float dt)
{
    if(count == 0)
    {
        countDown_layer->setVisible(false);
        rootNode->setColor(Color3B(255,255,255));
        schedule(schedule_selector(HelloWorld::timer),1.0f,4,0);
        
        auto click_btn = static_cast<Sprite*>(rootNode->getChildByName("click-btn"));
        auto next_btn = static_cast<Sprite*>(next_layer->getChildByName("next-btn"));
        auto clickEventListener = EventListenerTouchOneByOne::create();
        clickEventListener->onTouchBegan = [this,click_btn,next_btn](Touch* t,Event* e){
            if(next_btn->getBoundingBox().containsPoint(t->getLocation()) && isSuc)
            {
                Director::getInstance()->replaceScene(HelloWorld::createScene());
            }
            if(click_btn->getBoundingBox().containsPoint(t->getLocation()) && !isSuc)
            {
                if(flag && !isEnd)
                {
                    if(this->index <= 25 && this->index > 0)
                    {
                        auto s = static_cast<Sprite*>(rootNode->getChildByName(StringUtils::format("Sprite_%d",this->index++)));
                        s->setVisible(true);
                    }else
                    {
                        this->index2 = 25;
                        flag = false;
                        
                        schedule(schedule_selector(HelloWorld::push),0.2f,25,0);
                        unschedule(schedule_selector(HelloWorld::timer));
                    }
                }
            }
            return false;
        };
        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(clickEventListener, this);

    }
    else
    {
        auto countText = static_cast<cocos2d::ui::Text*>(countDown_layer->getChildByName("count-text"));
        countText->setString(StringUtils::format("%d",count));
        count --;
    }

}

void HelloWorld::timer(float dt)
{
    if(curTime == 0)
    {
        if(flag)
        {
            isEnd = true;
            Director::getInstance()->replaceScene(EndScene::createScene());
        }else
        {
            schedule(schedule_selector(HelloWorld::push),0.2f,24,0);
        }
    }
    auto timeText =static_cast<cocos2d::ui::Text*>(rootNode->getChildByName("time"));
    timeText->setString(StringUtils::format("%d",curTime));
    curTime--;
}


